package com.sunny.ynhz.utils;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created by huhuan123 on 2018/8/31.
 *
 * @describe TODO
 */

public class MeasureViewUtils {
    private static int mHigh = 0;

    public interface IResultMeasure {
        void resultHigh(int high);
    }

    public static void getMeasureViewHight(View view, IResultMeasure iResultMeasure) {
        mHigh = 0;
        ViewTreeObserver observer = view.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                if (mHigh != 0) {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                } else {
                    mHigh = view.getHeight();
                }
                iResultMeasure.resultHigh(mHigh);
            }
        });

    }
}
